package cn.sfa.common.interceptor;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import cn.sfa.common.utils.CommonUtil;
import cn.sfa.common.utils.HttpReyUtil;
import cn.sfa.entity.users.SaleUsersLogin;
/**
 * 
* 类名称：AuthorizeInterceptor   
* 类描述： 用户的权限认证过滤器
* 创建人：lenovo   
* 创建时间：2012-2-9 上午10:23:50     
* 修改备注：   
* @version v1.0
 */
public class AuthorizeInterceptor extends HandlerInterceptorAdapter {
	
	Logger log = LoggerFactory.getLogger(AuthorizeInterceptor.class);
	
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		
		log.info("进入权限判断拦截器");
		
		HttpSession session = request.getSession();
		SaleUsersLogin user =  (SaleUsersLogin) session.getAttribute("user");
		String curPath = request.getRequestURI().toString();
		
		request.setAttribute("curPath", curPath);
		request.setAttribute("path", request.getContextPath());

		//登录界面不需要进行权限权限控制
		if (curPath.indexOf("login")>=0){
	            return true;
	    }
		
		//如果会话过期则给予提示，跳转到登录页面
		if(user==null)
		{
			response.sendRedirect(request.getContextPath()+"/common/alter.jsp");//重定向登录提示页面
			return false;
		}
		
		List<XmlBean> list = XmlURLMerge.getXmlUrlList();

		if(curPath.indexOf("?")>0){//如果包含?,则把?以及后面的截掉
			curPath = curPath.substring(0,curPath.indexOf("?"));
		}
		boolean isPass = true;  //本来  boolean isPass = false; 
		for(XmlBean xml : list){
			if(curPath.endsWith(xml.getURL())){//如果当前链接以配置文件的相关信息接尾
				isPass=true;
				break;
			}else{//如果当前链接带有/${a}/${b}/..的参数,则进行最少匹配
				//当前curPath的前面一段
				String prestr = curPath.substring(0,curPath.indexOf("index"));
				String bstr = curPath.substring(curPath.indexOf("index"));
				//设定其后面存在一个方法,或者无存在方法(默认的不需要方法名称)
				if("index".equals(bstr)){//默认的
					if(curPath.endsWith(xml.getURL())){
						isPass=true;
						break;
					}
				}else{//后面有参数方法和参数
					String methodstr = bstr.split("/")[1];//设定第一个为 参数名
					String path = prestr+"index/"+methodstr;
					if(path.endsWith(xml.getURL())){
						isPass=true;
						break;
					}
				}
			}
		}
		
		request.setAttribute("isPass", isPass);
		
		//如果当前路径在url列表中,则可以进行操作,否则给予提示
		
		if(CommonUtil.isEmpty(user)){
			response.sendRedirect(request.getContextPath()+"/sfa/index.jsp");//重定向登录页面
		}
		
		return super.preHandle(request, response, handler);
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		
		
		
		String userAgent = request.getHeader("User-Agent");
		
		String judgeValue= HttpReyUtil.isFromMobile(userAgent);
		
		boolean  isPass = true;
		if(!CommonUtil.isEmpty(request.getAttribute("isPass")))
		  isPass = (Boolean) request.getAttribute("isPass");
		
		if(!isPass){
			
			request.setAttribute("msg","对不起,您没有操作此项功能的权限!" );
			modelAndView.setViewName("index");
			
			
		}else{
		
			/*if(judgeValue.startsWith("mobile")){
				modelAndView.setViewName("mobile/"+modelAndView.getViewName());
			}*/
		}
		
		log.info("uri================="+request.getRequestURI());

		//response.sendRedirect(request.getContextPath()+"/routine/calendar/index/show");
		
		String uri = request.getRequestURI();
		 
		if("/routine/index".equals(uri)){
			//个人事务
			request.getRequestDispatcher("/routine/calendar/index/show").forward(request, response);
		}else if("/customer/index".equals(uri)){
			//客户管理
			request.getRequestDispatcher("/customer/manage/index/getcustomers").forward(request, response); 
		}else if("/team/index".equals(uri)){
			//团队管理
			request.getRequestDispatcher("/team/team/index/showteam").forward(request, response); 
		}else if("/business/index".equals(uri)){
			//业务管理
			request.getRequestDispatcher("/business/poldetail/index/acceptlist").forward(request, response); 
		}//else if("/sfa/report/index".equals(uri)){
			//业务报表
			//request.getRequestDispatcher("/report/base/index/yearachievement").forward(request, response);
			
		//}
		else if("/users/index".equals(uri)){
			//系统设置
			request.getRequestDispatcher("/users/users/index/getusers").forward(request, response); 
		}
		else if("/manage/index".equals(uri)){
			//后台管理
			request.getRequestDispatcher("/manage/role/index/getroles").forward(request, response); 
		}
		else if("/productandtrain/index".equals(uri)){
			//产品与培训 
			request.getRequestDispatcher("/productandtrain/lecturer/index").forward(request, response); 
		}
		
		else if("/sms/index".equals(uri)){  
			//短信管理
			request.getRequestDispatcher("/sms/index/message").forward(request, response);  
		}
		super.postHandle(request, response, handler, modelAndView);
	} }
